package com.leyou.item.controller;

import com.leyou.common.dto.CartDTO;
import com.leyou.common.vo.PageResult;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.SpuBo;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {

    @Autowired
    public GoodsService goodsService;

    /**
     * 分页查询商品数据
     *
     * @param key
     * @param page
     * @param rows
     * @param saleable
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> querySpuPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable) {

        return ResponseEntity.ok(goodsService.querySpuPage(key, page, rows, saleable));
    }

    /**
     * 新增商品数据
     *
     * @param spuBo
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGood(@RequestBody SpuBo spuBo) {
        goodsService.saveGood(spuBo);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 修改商品数据
     *
     * @param spuBo
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGood(@RequestBody SpuBo spuBo) {
        goodsService.updateGood(spuBo);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid查询spu详情数据
     *
     * @param spuId
     * @return
     */
    @GetMapping("spu/detail/{spuId}")
    public ResponseEntity<SpuDetail> querySpuDetailBySpuId(@PathVariable("spuId") Long spuId) {

        return ResponseEntity.ok(goodsService.querySpuDetailBySpuId(spuId));

    }

    /**
     * 根据spuid查询sku集合数据
     *
     * @param spuId
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuBySpuId(@RequestParam("id") Long spuId) {

        return ResponseEntity.ok(goodsService.querySkuBySpuId(spuId));

    }

    /**
     * 根据skuIds查询sku集合数据
     *
     * @param skuIds
     * @return
     */
    @GetMapping("sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuBySkuIds(@RequestParam("ids") List<Long> skuIds) {

        return ResponseEntity.ok(goodsService.querySkuBySkuIds(skuIds));
    }

    /**
     * 根据spuid查询spu
     *
     * @param spuId
     * @return
     */
    @GetMapping("spu/{spuId}")
    public ResponseEntity<SpuBo> querySpuBySpuId(@PathVariable("spuId") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuBySpuId(spuId));
    }

    /**
     * 减库存
     *
     * @param cartDTOList
     * @return
     */
    @PostMapping("stock/decrease")
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDTO> cartDTOList) {

        goodsService.decreaseStock(cartDTOList);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
